<?php

namespace app\api\controller\app;

use app\admin\model\Article;
use app\admin\model\Articletype;
use app\admin\model\City;
use app\admin\model\Shop;
use app\admin\model\Shopjoin;
use app\api\service\MpService;
use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Http;
use fast\Random;
use think\Config;
use think\Validate;

/**
 */
class System extends Api
{
    protected $noNeedLogin = ['addShopJoin', 'articelLis'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

    }

    /**
     * 加盟信息
     */
    public function addShopJoin()
    {

        $city = $this->request->param('city');
        $name = $this->request->param('name');
        $mobile = $this->request->param('mobile');
        $org_id = $this->request->param('org_id');
        $isLocal = $this->request->param('isLocal');
        $isBudget = $this->request->param('isBudget');
        $isProgram = $this->request->param('isProgram');
        $ref_mobile = $this->request->param('ref_mobile');

        $loclArr = ['0' => '有场地', '1' => '无场地'];
        $budgetArr = ['0' => '10-15万', '1' => '20-30万', '2' => '35-50万以上'];
        $programArr = ['0' => '棋牌', '1' => '台球', '2' => 'KTV·影吧'];

        $localTxt = $loclArr[$isLocal];
        $budgetTxt = $budgetArr[$isBudget];

        $newProgramArr = array_intersect_key($programArr, array_flip(json_decode($isProgram, true)));
        $programTxt = implode(',', $newProgramArr);


        if ($name == "") {
            $this->error("请输入您的姓名");
        }
        if ($mobile == "") {
            $this->error("请输入您的手机号");
        }

        $pattern = "/^1[3456789]{1}\d{9}$/";
        $isPhone = preg_match($pattern, $mobile);
        if (!$isPhone) {
            $this->error("请输入正确的手机号");

        }

        $shopJoinModel = new Shopjoin();
        $shopJoinModel->insert(['city' => $city,'ref_mobile'=>$ref_mobile, 'name' => $name, 'mobile' => $mobile, 'create_time' => time(),
            'org_id' => $org_id, 'is_shop' => $isLocal, 'budget' => $isBudget, 'item_data' => $programTxt]);

        $ids = array();
        $ids[] = "oV1gG6RrSwxyTivsLZDs1jYBMBn4";
        $ids[] = "oV1gG6Z6dUTZbtGKRDghUMa9VfBw";
        $ids[] = "oV1gG6c7bwXQXcE8cWIIE7scMK2I";//陈伟琴

        $mpService = new MpService();
        foreach ($ids as $key => $val) {

            $param = array();
            $param['openid'] = $val;
            $param['name'] = $name . $mobile;
            $param['time'] = date("Y-m-d H:i");
            $mpService->joinNotify($this->wxApp, $param);
        }


        $this->success('多谢提交，我们会尽快联系您');
    }

    public function articelList()
    {
        $articeType = new Articletype();
        $articleModel = new Article();
        $list = $articeType->select();
        foreach ($list as $key => $val) {
            $list[$key]['contentList'] = $articleModel->where('type_id', $val['id'])->select();
        }
        $this->success('', $list);
    }

    public function articleInfo()
    {
        $articleModel = new Article();
        $name = $this->request->param('name');
        $article = $articleModel->where('name', $name)->find();
        $this->success('', $article);
    }


}
